public class MyLinkedQueue {
    //底层为双向链表
    static class ListNode {

        int val;
        ListNode prev;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //定义头尾指针
    ListNode head;
    ListNode last;

    //入队
    public void offer(int val) {
        //尾插法
        //如果队列为空，新建结点并让头尾指针指向它
        ListNode newNode = new ListNode(val);
        if(isEmpty()) {
            head = newNode;
            last = newNode;
        }else {
            newNode.next = last.next;
            last.next = newNode;
            newNode.prev = last;
            last = newNode;
        }
    }

    //出队
    public int poll() {
         //判空，队列为空直接抛出异常
        if(isEmpty()) {
            throw new RuntimeException("队列为空！");
        }

        int value = last.val;
        last.prev.next = last.next;
        last = last.prev;

        return value;
    }

    //判空
    public boolean isEmpty() {
        return this.head == null;
    }

    //打印队列
    public void disPlay() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }


}
